package com.link510.aitools.services;


import com.link510.aitools.core.data.model.QiniuFileListingModel;
import com.link510.aitools.core.errors.SateCollect;
import com.link510.aitools.core.listen.CWMListener;
import com.link510.aitools.core.message.MessageInfo;
import com.link510.aitools.data.AirobotDtata;
import com.orhanobut.logger.Logger;

import io.reactivex.schedulers.Schedulers;

/**
 * AI智能机器人
 */
public class Airobots extends BaseService {

    /**
     * 智能机器人
     */
    private static Airobots mInstance = null;

    public static Airobots getInstance() {

        if (mInstance == null) {
            synchronized (Airobots.class) {
                if (mInstance != null) {
                    return mInstance;
                }
                mInstance = new Airobots();
            }
        }

        return mInstance;
    }

    private Airobots() {
    }

    //region 核心板


    /**
     * 修改设备编号
     *
     * @param deviceSN 设备编号
     * @param name     设备名称
     */
    public void editName(String deviceSN,
                         String name, CWMListener<MessageInfo<String>> listener) {

        try {
            AirobotDtata.getInstance().editName(deviceSN, name)
                    .subscribeOn(Schedulers.newThread())
                    .subscribe(messageInfo -> {

                        if (messageInfo == null) {
                            onErrorAction(listener, -3, "数据消息解析异常");
                            return;
                        }

                        if (!SateCollect.SUCCESS.equals(messageInfo.getState())) {
                            onErrorAction(listener, messageInfo.getState(), messageInfo.getMessage());
                            return;
                        }

                        onSuccessAction(listener, messageInfo.getMessage(), messageInfo);

                    }, t -> {
                        // 处理异常

                        try {
                            onErrorAction(listener, -2, "发送命令发生异常");
                            Logger.e(t, "发送命令发生异常");
                        } catch (Exception ex) {
                            ex.printStackTrace();
                        }
                    });
        } catch (Exception ex) {

            ex.printStackTrace();
            Logger.e(ex, "发送命令发生异常");
            onErrorAction(listener, -1, "发送命令发生异常");
        }
    }

    /**
     * 修改设备编号
     *
     * @param deviceSN 设备编号
     * @param ip       设备IP
     * @param port     设备端口
     */
    public void editIpAndPort(String deviceSN,
                              String ip,
                              Integer port, CWMListener<MessageInfo<String>> listener) {

        try {
            AirobotDtata.getInstance().editIpAndPort(deviceSN, ip, port)
                    .subscribeOn(Schedulers.newThread())
                    .subscribe(messageInfo -> {

                        if (messageInfo == null) {
                            onErrorAction(listener, -3, "数据消息解析异常");
                            return;
                        }

                        if (!SateCollect.SUCCESS.equals(messageInfo.getState())) {
                            onErrorAction(listener, messageInfo.getState(), messageInfo.getMessage());
                            return;
                        }

                        onSuccessAction(listener, messageInfo.getMessage(), messageInfo);

                    }, t -> {
                        // 处理异常

                        try {
                            onErrorAction(listener, -2, "发送命令发生异常");
                            Logger.e(t, "发送命令发生异常");
                        } catch (Exception ex) {
                            ex.printStackTrace();
                        }
                    });
        } catch (Exception ex) {

            ex.printStackTrace();
            Logger.e(ex, "发送命令发生异常");
            onErrorAction(listener, -1, "发送命令发生异常");
        }
    }


    /**
     * 固件更新
     *
     * @param deviceSN 设备编号
     * @param httpUrl  固件地址
     * @param listener 临听
     */
    public void coreUpgradeSend(String deviceSN, String httpUrl, CWMListener<MessageInfo<String>> listener) {
        try {

            AirobotDtata.getInstance().coreUpgradeSend(deviceSN, httpUrl).subscribeOn(Schedulers.newThread())
                    .subscribe(messageInfo -> {

                        if (messageInfo == null) {
                            onErrorAction(listener, -3, "数据消息解析异常");
                            return;
                        }

                        if (!SateCollect.SUCCESS.equals(messageInfo.getState())) {
                            onErrorAction(listener, messageInfo.getState(), messageInfo.getMessage());
                            return;
                        }

                        onSuccessAction(listener, messageInfo.getMessage(), messageInfo);

                    }, t -> {
                        // 处理异常

                        try {
                            onErrorAction(listener, -2, "发送命令发生异常");
                            Logger.e("发送命令发生异常");
                        } catch (Exception ex) {
                            ex.printStackTrace();
                        }
                    });

        } catch (Exception ex) {
            Logger.e(ex, "发送命令发生异常");
            onErrorAction(listener, -1, "发送命令发生异常");
        }
    }


    /**
     * 日志收集
     *
     * @param deviceSN 设备编号
     * @param listener listener监听器
     */
    public void logsGet(String deviceSN, CWMListener<MessageInfo<String>> listener) {
        try {

            AirobotDtata.getInstance().logsGet(deviceSN).subscribeOn(Schedulers.newThread())
                    .subscribe(messageInfo -> {

                        if (messageInfo == null) {
                            onErrorAction(listener, -3, "数据消息解析异常");
                            return;
                        }

                        if (!SateCollect.SUCCESS.equals(messageInfo.getState())) {
                            onErrorAction(listener, messageInfo.getState(), messageInfo.getMessage());
                            return;
                        }

                        onSuccessAction(listener, messageInfo.getMessage(), messageInfo);

                    }, t -> {
                        // 处理异常

                        try {
                            onErrorAction(listener, -2, "发送命令发生异常");
                            Logger.e("发送命令发生异常");
                        } catch (Exception ex) {
                            ex.printStackTrace();
                        }
                    });

        } catch (Exception ex) {
            Logger.e(ex, "发送命令发生异常");
            onErrorAction(listener, -1, "发送命令发生异常");
        }
    }


    /**
     * 重启设备
     *
     * @param deviceSN 设备编号
     * @param listener 监听
     */
    public void reboot(String deviceSN, CWMListener<MessageInfo<String>> listener) {


        try {
            AirobotDtata.getInstance().reboot(deviceSN).subscribeOn(Schedulers.newThread())
                    .subscribe(messageInfo -> {

                        if (messageInfo == null) {
                            onErrorAction(listener, -3, "数据消息解析异常");
                            return;
                        }

                        if (!SateCollect.SUCCESS.equals(messageInfo.getState())) {
                            onErrorAction(listener, messageInfo.getState(), messageInfo.getMessage());
                            return;
                        }

                        onSuccessAction(listener, messageInfo.getMessage(), messageInfo);

                    }, t -> {
                        // 处理异常

                        try {
                            onErrorAction(listener, -2, "发送命令发生异常");
                            Logger.e(t, "发送命令发生异常");
                        } catch (Exception ex) {
                            ex.printStackTrace();
                        }
                    });
        } catch (Exception ex) {

            ex.printStackTrace();
            Logger.e(ex, "发送命令发生异常");
            onErrorAction(listener, -1, "发送命令发生异常");
        }

    }

    //endregion


    //region LED

    /**
     * led文本修改
     *
     * @param deviceSN 设备编号
     * @param text     文本内容
     * @param listener 监听器
     */
    public void ledEditText(String deviceSN, String text, CWMListener<MessageInfo<String>> listener) {

        try {

            AirobotDtata.getInstance().ledEditText(deviceSN, text).subscribeOn(Schedulers.newThread())
                    .subscribe(messageInfo -> {

                        if (messageInfo == null) {
                            onErrorAction(listener, -3, "数据消息解析异常");
                            return;
                        }

                        if (!SateCollect.SUCCESS.equals(messageInfo.getState())) {
                            onErrorAction(listener, messageInfo.getState(), messageInfo.getMessage());
                            return;
                        }

                        onSuccessAction(listener, messageInfo.getMessage(), messageInfo);

                    }, t -> {
                        // 处理异常

                        try {
                            onErrorAction(listener, -2, "发送命令发生异常");
                            Logger.e(t, "发送命令发生异常");
                        } catch (Exception ex) {
                            ex.printStackTrace();
                        }
                    });

        } catch (Exception ex) {
            Logger.e(ex, "发送命令发生异常");
            onErrorAction(listener, -1, "发送命令发生异常");
        }


    }

    //endregion

    //region 图片或文件

    /**
     * 获取图片
     *
     * @param deviceSN 设备编号
     * @param time     时间戳
     * @param marker   maker
     * @param limit    长度
     * @return Observable<MessageInfo < QiniuFileListingModel>>
     */
    public void pictureList(String deviceSN,
                            Integer time,
                            String marker,
                            Integer limit,
                            CWMListener<MessageInfo<QiniuFileListingModel>> listener) {

        try {

            AirobotDtata.getInstance().pictureList(deviceSN, time, marker, limit).subscribeOn(Schedulers.newThread())
                    .subscribe(messageInfo -> {

                        if (messageInfo == null) {
                            onErrorAction(listener, -3, "数据消息解析异常");
                            return;
                        }

                        if (!SateCollect.SUCCESS.equals(messageInfo.getState())) {
                            onErrorAction(listener, messageInfo.getState(), messageInfo.getMessage());
                            return;
                        }

                        onSuccessAction(listener, messageInfo.getMessage(), messageInfo);

                    }, t -> {
                        // 处理异常

                        try {
                            onErrorAction(listener, -2, "发送命令发生异常");
                            Logger.e(t, "发送命令发生异常");
                        } catch (Exception ex) {
                            ex.printStackTrace();
                        }
                    });

        } catch (Exception ex) {
            Logger.e(ex, "发送命令发生异常");
            onErrorAction(listener, -1, "发送命令发生异常");
        }
    }


    //endregion

    //region 音频模块


    /**
     * 音频下发中
     *
     * @param mp3Url   mp3URL地址
     * @param listener 监听
     */
    public void audioEditUrl(String deviceSN, String mp3Url, CWMListener<MessageInfo<String>> listener) {
        try {

            AirobotDtata.getInstance().audioEditUrl(deviceSN, mp3Url).subscribeOn(Schedulers.newThread())
                    .subscribe(messageInfo -> {

                        if (messageInfo == null) {
                            onErrorAction(listener, -3, "数据消息解析异常");
                            return;
                        }

                        if (!SateCollect.SUCCESS.equals(messageInfo.getState())) {
                            onErrorAction(listener, messageInfo.getState(), messageInfo.getMessage());
                            return;
                        }

                        onSuccessAction(listener, messageInfo.getMessage(), messageInfo);

                    }, t -> {
                        // 处理异常

                        try {
                            onErrorAction(listener, -2, "发送命令发生异常");
                            Logger.e("发送命令发生异常");
                        } catch (Exception ex) {
                            ex.printStackTrace();
                        }
                    });

        } catch (Exception ex) {
            Logger.e(ex, "发送命令发生异常");
            onErrorAction(listener, -1, "发送命令发生异常");
        }
    }

    /**
     * 远程喊话
     *
     * @param deviceSN 设备编码
     * @param mp3Url   mp3地址
     * @param listener 监听
     */
    public void audioShoutingUrl(String deviceSN, String mp3Url, CWMListener<MessageInfo<String>> listener) {
        try {

            AirobotDtata.getInstance().audioShoutingUrl(deviceSN, mp3Url).subscribeOn(Schedulers.newThread())
                    .subscribe(messageInfo -> {

                        if (messageInfo == null) {
                            onErrorAction(listener, -3, "数据消息解析异常");
                            return;
                        }

                        if (!SateCollect.SUCCESS.equals(messageInfo.getState())) {
                            onErrorAction(listener, messageInfo.getState(), messageInfo.getMessage());
                            return;
                        }

                        onSuccessAction(listener, messageInfo.getMessage(), messageInfo);

                    }, t -> {
                        // 处理异常

                        try {
                            onErrorAction(listener, -2, "发送命令发生异常");
                            Logger.e("发送命令发生异常");
                        } catch (Exception ex) {
                            ex.printStackTrace();
                        }
                    });

        } catch (Exception ex) {
            Logger.e(ex, "发送命令发生异常");
            onErrorAction(listener, -1, "发送命令发生异常");
        }
    }


    //endregion

}
